var os = require("os");
var path = require("path");
var fs = require("fs");
var author = /[^\\]+$(?!\\)/.exec(os.homedir())[0];
var versionFilePath = path.join(__dirname,'./versions-list.json')
var versionsData= require(versionFilePath);
var nowDate = new Date;
var versions=versionsData.versions;
var argVersionIndex;
var hasVersion=process.argv.forEach(function(a,index){
  if(!!a&&a.match(/version/)){argVersionIndex=index;};
})
var versionName=''
if(argVersionIndex&&process.argv[argVersionIndex+1]){
versionName=process.argv[argVersionIndex+1]
}
if(versions&&versions.slice){
  var currentVersion=versions[versions.length-1];
  var nextVersion={
        "version": null,
        "date": nowDate.toLocaleDateString() + " "+ nowDate.toLocaleTimeString(),
        "author": author
  }
  if(versionName!=''){
    nextVersion.version=versionName
  }else{
    if(!currentVersion||currentVersion.length==0){
      nextVersion.version='0.0.0.1'
    }else {
      var lastVersion =/[^\.]+$(?!\.)/.exec(currentVersion.version)[0];
      var lastReg = new RegExp(lastVersion+"$");
      var newLastVersion = (parseInt(lastVersion)+1)
      nextVersion.version=currentVersion.version.replace(lastReg,newLastVersion)
    }
  }

  versions.push(nextVersion)
  fs.writeFileSync(versionFilePath,JSON.stringify(versionsData,null,4))

}
